package club.caihappy.words.Servlet;


import jxl.Workbook;
import jxl.write.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;


@RestController
@RequestMapping("/Words")
public class Json2Excel
        extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @RequestMapping("/J2E")
    public void service(@RequestParam("filename") String name,@RequestParam("data") String str, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("FileName: " + name);

        FileOutputStream output = null;
        try {
            long date = (new Date()).getTime();
            String realPath = "/home/" + date;
//            String realPath = pathStatic.replace('/', '\\').substring(1, pathStatic.length());

            System.out.print(realPath);
            File f = new File(realPath);
            if (!f.exists()) {
                f.mkdirs();
            }

            output = new FileOutputStream(realPath + "/" + name + ".xls");
            createExcel(output, str);
            PrintWriter out = response.getWriter();
            out.print("/upload/" + date);
        } catch (WriteException | IOException e1) {

            e1.printStackTrace();
        }
    }


    public void createExcel(OutputStream os, String data) throws WriteException, IOException {
        WritableWorkbook workbook = Workbook.createWorkbook(os);


        WritableSheet sheet = workbook.createSheet("Sheet1", 0);

        Label word = new Label(0, 0, "Word");
        sheet.addCell((WritableCell) word);
        Label speech = new Label(1, 0, "Speech");
        sheet.addCell((WritableCell) speech);
        Label translation = new Label(2, 0, "Translation");
        sheet.addCell((WritableCell) translation);
        JSONArray json = JSONArray.fromObject(data);

        if (json.size() > 0) {
            for (int i = 0; i < json.size(); i++) {
                JSONObject job = json.getJSONObject(i);
                Label qinghua = new Label(0, i + 1, (String) job.get("Word"));
                sheet.addCell((WritableCell) qinghua);
                Label jisuanji = new Label(1, i + 1, (String) job.get("Speech"));
                sheet.addCell((WritableCell) jisuanji);
                Label gao = new Label(2, i + 1, (String) job.get("Translation"));
                sheet.addCell((WritableCell) gao);
            }
        }


        workbook.write();
        workbook.close();
        os.flush();
        os.close();
    }
}

